﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using SEVN.Web.Framework.Controls;
using System.Web.UI.HtmlControls;

namespace SEVN.Web.Framework
{
    // Lớp Control thực trên các trang Web
    public class ControlOnPage
    {

        private MyControl controlPage;

        public MyControl ControlPage
        {
            get { return controlPage; }
            set { controlPage = value; }
        }

        private char position;

        public char Position
        {
            get { return position; }
            set { position = value; }
        }

        private int order;

        public int Order
        {
            get { return order; }
            set { order = value; }
        }

        public ControlOnPage()
        {
        }

        public ControlOnPage(MyControl controlPage, char position, int order)
        {
            this.controlPage = controlPage;
            this.position = position;
            this.order = order;
        }
    }

    public class Ultility
    {
        #region 1. Methods for MyPage

        /// <summary>
        /// Phương thức lấy về toàn bộ Control trên 1 trang (Tính cả trên Skin)
        /// </summary>
        /// <param name="pg">Trang cần lấy Control</param>
        /// <returns>Danh sách các Control trên trang</returns>
        private static List<MyControl> GetListMyControlOnPage(MyPage pg)
        {
            List<MyControl> list = new List<MyControl>();

            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                if (pg != null)
                {
                    Skin sk = pg.Skin;
                    foreach (var item in sk.SkinControls)
                    {
                        list.Add(item.MyControl);
                    }

                    foreach (var item in pg.PageControls)
                    {
                        list.Add(item.MyControl);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// Phương thức lấy về tất cả Control có thể đưa vào trang Web
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        private static List<MyControl> GetAllControlToList(MyPage pg)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<MyControl> listControl = new List<MyControl>();
                if (pg.Position != null)
                {
                    foreach (MyControl ct in db.MyControls)
                    {
                        bool isAdd = false;
                        foreach (char ch in pg.Position)
                        {
                            bool check = false;
                            foreach (char c in ct.Position)
                            {
                                if (ch == c)
                                {
                                    check = true;
                                    break;
                                }
                            }

                            if (check)
                            {
                                isAdd = true;
                                break;
                            }
                        }

                        if (isAdd)
                            listControl.Add(ct);
                    }

                }

                return listControl;
            }
        }

        public static List<MyControl> GetAvailableControlToList(MyPage pg)
        {
            List<MyControl> listResult = new List<MyControl>();
            List<MyControl> listOnPage = GetListMyControlOnPage(pg);
            List<MyControl> listAllControls = GetAllControlToList(pg);

            foreach (MyControl item in listAllControls)
            {
                bool check = true;
                foreach (MyControl item2 in listOnPage)
                {
                    if (item.ControlID == item2.ControlID)
                    {
                        check = false;
                        break;
                    }
                }

                if (check)
                    listResult.Add(item);

            }

            return listResult;
        }


        public static List<MyControl> GetAvailableControlToPageByModule(MyPage pg, int moduleID)
        {
            List<MyControl> listResult = new List<MyControl>();
            List<MyControl> listOnPage = GetAvailableControlToList(pg);

            foreach (MyControl item in listOnPage)
            {
                if (item.ModuleID == moduleID)
                {
                    listResult.Add(item);
                }
            }

            return listResult;
        }

        public static List<Module> GetModulesByPosition(char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<Module> list = new List<Module>();
                foreach (var item in db.Modules)
                {
                    if (item.Position.Contains(position))
                        list.Add(item);
                }
                return list;
            }
        }

        public static List<Position> GetPositionsBySkin(Skin sk)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<Position> list = new List<Position>();
                foreach (var item in db.Positions)
                {
                    if (sk.Position.Contains(item.PositionID))
                        list.Add(item);
                }
                return list;
            }
        }

        private static List<ControlOnPage> GetListControlOnPage(MyPage pg)
        {
            List<ControlOnPage> list = new List<ControlOnPage>();

            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                if (pg != null)
                {
                    Skin sk = pg.Skin;
                    foreach (var item in sk.SkinControls)
                    {
                        ControlOnPage cop = new ControlOnPage(item.MyControl, item.Position.Value, item.ControlOrder);
                        list.Add(cop);
                    }

                    foreach (var item in pg.PageControls)
                    {
                        ControlOnPage cop = new ControlOnPage(item.MyControl, item.Position.Value, item.ControlOrder);
                        list.Add(cop);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// Phương thức lấy về danh sác các Control trên trang theo vị trí
        /// </summary>
        /// <param name="pg"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static List<ControlOnPage> GetListControl(MyPage pg, char position)
        {
            List<ControlOnPage> list = GetListControlOnPage(pg);
            List<ControlOnPage> listControl = new List<ControlOnPage>();

            foreach (var item in list)
            {
                if (item.Position == position)
                    listControl.Add(item);
            }

            return listControl;
        }

        public static List<ControlOnPage> GetListControl(List<ControlOnPage> listAllControlOnPage, char position)
        {
            List<ControlOnPage> listControl = new List<ControlOnPage>();

            foreach (var item in listAllControlOnPage)
            {
                if (item.Position == position)
                    listControl.Add(item);
            }

            return listControl;
        }

        public static void LoadControlToPage(Page pg, PlaceHolder placeHoder, ControlOnPage control)
        {
            Control webControl = new Control();
            webControl = pg.LoadControl(control.ControlPage.VirtualPath);
            placeHoder.Controls.Add(webControl);
        }

        public static void LoadControlToPageForAdmin(Page pg, PlaceHolder placeHoder, ControlOnPage control)
        {
            HtmlGenericControl gen = new HtmlGenericControl("div");
            gen.Attributes["id"] = control.ControlPage.ControlCode;
            gen.Attributes["class"] = "control";

            HtmlGenericControl genTitle = new HtmlGenericControl("div");
            genTitle.Attributes["class"] = "titleControl";
            gen.Controls.Add(genTitle);

            Control webControl = new Control();
            webControl = pg.LoadControl(control.ControlPage.VirtualPath);
            gen.Controls.Add(webControl);
            placeHoder.Controls.Add(gen);
        }



        public static void LoadAllControlToPage(Page pg, PlaceHolder placeHoder, MyPage myPage, char position)
        {
            List<ControlOnPage> list = GetListControl(myPage, position);

            list = list.OrderBy(i => i.Order).ToList();

            foreach (ControlOnPage item in list)
            {
                LoadControlToPage(pg, placeHoder, item);
            }
        }

        public static void LoadAllControlToPageForAdmin(Page pg, PlaceHolder placeHoder, MyPage myPage, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<ControlOnPage> list = GetListControl(myPage, position);

                list = list.OrderBy(i => i.Order).ToList();

                foreach (ControlOnPage item in list)
                {
                    LoadControlToPageForAdmin(pg, placeHoder, item);
                }
            }
        }

        public static int GetMaxOrder(MyPage myPage, char position)
        {
            int max = 0;
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                var data = db.PageControls.Where(p => p.MyPage == myPage && p.Position == position).ToList();
                if (data.Count() > 0)
                    max = data.Max(p => p.ControlOrder);
            }

            return max;
        }

        public static int GetMinOrder(MyPage myPage, char position)
        {
            int min = 0;
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                var data = db.PageControls.Where(p => p.MyPage == myPage && p.Position == position).ToList();
                if (data.Count() > 0)
                    min = data.Min(p => p.ControlOrder);
            }

            return min;
        }

        /// <summary>
        /// Lấy về vị trí của Control theo tên Panel chứa
        /// </summary>
        /// <param name="container">Tên panel chứa bắt đầu bằng pnl</param>
        /// <returns></returns>
        public static char GetPosition(string container)
        {

            if (container.Length >= 4)
                return container[3];
            else
                return ' ';

        }

        public static ControlOnPage GetControlOnPageByCode(MyPage page, string controlCode)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl control = db.PageControls.SingleOrDefault(p => p.MyPage == page && p.MyControl.ControlCode == controlCode);

                if (control != null)
                {
                    ControlOnPage cop = new ControlOnPage(control.MyControl, control.Position.Value, control.ControlOrder);
                    return cop;
                }
                else
                    return null;
            }
        }

        public static void ReorderControlOnPage(MyPage myPage, string controlCode, int newOrder)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {

                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.MyControl.ControlCode == controlCode);
                //su ly truong hop su ly tren page
                //Truong hop keo cung 1 ben
                //Kiem tra vi tri cu va moi
                var oldOrder = pc.ControlOrder;
                //Vi tri cu lon hon vi tri moi ==> keo len tren
                //
                if (oldOrder > newOrder)
                {
                    //Lay nhung control bi thay doi tu vi tri moi toi vi tri cu
                    var allControlChange = db.PageControls.Where(p => p.MyPage == myPage && p.ControlOrder >= newOrder && p.ControlOrder < oldOrder);
                    foreach (var item in allControlChange)
                    {
                        item.ControlOrder = item.ControlOrder + 1;
                    }

                }
                //Khong thay doi vi tri
                else if (oldOrder == newOrder)
                {
                    //Khong co thay doi gi
                }
                //Vi tri cu nho hon vi tri moi keo xuong duoi
                else
                {
                    //Lay nhung control bi thay doi tu vi tri cu toi vi tri moi
                    var allControlChange = db.PageControls.Where(p => p.MyPage == myPage && p.ControlOrder <= newOrder && p.ControlOrder > oldOrder);
                    foreach (var item in allControlChange)
                    {
                        item.ControlOrder = item.ControlOrder - 1;
                    }
                }
                pc.ControlOrder = newOrder;
                //var listControl = db.PageControls.Where(p => p.MyPage == myPage && p.Position == newPosition && p.ControlOrder <= newOrder);

                //pc.ControlOrder = newOrder;
                //pc.Position = newPosition;

                db.SubmitChanges();



            }
        }

        public static void UpOrderControl(MyPage myPage, ControlOnPage control, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.ControlID == control.ControlPage.ControlID && p.Position == position);
                PageControl pc2 = db.PageControls.Where(p => p.MyPage == myPage && p.Position == position && p.ControlOrder < pc.ControlOrder).OrderByDescending(p => p.ControlOrder).FirstOrDefault();
                if (pc2 != null)
                {
                    int order = pc.ControlOrder;
                    pc.ControlOrder = pc2.ControlOrder;
                    pc2.ControlOrder = order;

                    db.SubmitChanges();
                }
            }
        }

        public static void DownOrderControl(MyPage myPage, ControlOnPage control, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.ControlID == control.ControlPage.ControlID && p.Position == position);
                PageControl pc2 = db.PageControls.Where(p => p.MyPage == myPage && p.Position == position && p.ControlOrder > pc.ControlOrder).OrderBy(p => p.ControlOrder).FirstOrDefault();
                if (pc2 != null)
                {
                    int order = pc.ControlOrder;
                    pc.ControlOrder = pc2.ControlOrder;
                    pc2.ControlOrder = order;

                    db.SubmitChanges();
                }
            }
        }

        /// <summary>
        /// Xóa một Control trên trang
        /// </summary>
        /// <param name="myPage">Mã điều khiển</param>
        /// <param name="controlCode">Mã điều khiển</param>
        /// <param name="position">Vị trí: Skin hoặc Page</param>
        /// <param name="totalControl">Tổng số Control cùng vì trí</param>
        public static void DeleteControl(MyPage myPage, string controlCode, char position, int totalControl)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.MyControl.ControlCode == controlCode);
                db.PageControls.DeleteOnSubmit(pc);

                //Thay đổi vị trí các Control còn lại
                if ((pc.ControlOrder + 1) != totalControl)
                {
                    var listUpdate = db.PageControls.Where(p => p.MyPage == myPage && p.Position == position && p.ControlOrder > pc.ControlOrder);

                    foreach (var item in listUpdate)
                    {
                        item.ControlOrder -= 1;
                    }
                }

                db.SubmitChanges();
            }
        }

        public static void MoveControlToLeft(MyPage myPage, ControlOnPage control)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.ControlID == control.ControlPage.ControlID && p.Position == 'R');
                if (pc != null)
                {
                    int maxOrder = GetMaxOrder(myPage, 'L');
                    pc.ControlOrder = maxOrder + 1;
                    pc.Position = 'L';

                    var list = db.PageControls.Where(p => p.MyPage == myPage && p.Position == 'R' && p.ControlOrder > pc.ControlOrder);
                    foreach (var item in list)
                    {
                        item.ControlOrder -= 1;
                    }

                    db.SubmitChanges();
                }
            }
        }

        public static void MoveControlToRight(MyPage myPage, ControlOnPage control)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                PageControl pc = db.PageControls.SingleOrDefault(p => p.MyPage == myPage && p.ControlID == control.ControlPage.ControlID && p.Position == 'L');
                if (pc != null)
                {
                    int maxOrder = GetMaxOrder(myPage, 'R');
                    pc.ControlOrder = maxOrder + 1;
                    pc.Position = 'R';
                    db.SubmitChanges();
                }
            }
        }

        #endregion

        #region 2. Methods for MySkin
        private static List<MyControl> GetListControlOnSkin(Skin sk)
        {
            List<MyControl> list = new List<MyControl>();

            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                if (sk != null)
                {
                    foreach (var item in sk.SkinControls)
                    {
                        list.Add(item.MyControl);
                    }
                }
            }

            return list;
        }

        private static List<MyControl> GetAllControlToList(Skin sk)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<MyControl> listControl = new List<MyControl>();
                if (sk != null)
                {
                    foreach (MyControl ct in db.MyControls)
                    {
                        if (!string.IsNullOrEmpty(ct.Position))
                        {
                            bool isAdd = false;
                            foreach (char ch in sk.Position)
                            {
                                bool check = false;
                                foreach (char c in ct.Position)
                                {
                                    if (ch == c)
                                    {
                                        check = true;
                                        break;
                                    }
                                }

                                if (check)
                                {
                                    isAdd = true;
                                    break;
                                }
                            }

                            if (isAdd)
                                listControl.Add(ct);
                        }
                    }

                }

                return listControl;
            }
        }

        public static List<MyControl> GetAvailableControlToList(Skin sk)
        {
            List<MyControl> listResult = new List<MyControl>();
            List<MyControl> listOnPage = GetListControlOnSkin(sk);
            List<MyControl> listAllControls = GetAllControlToList(sk);

            foreach (MyControl item in listAllControls)
            {
                bool check = true;
                foreach (MyControl item2 in listOnPage)
                {
                    if (item.ControlID == item2.ControlID)
                    {
                        check = false;
                        break;
                    }
                }

                if (check)
                    listResult.Add(item);

            }

            return listResult;
        }

        public static List<MyControl> GetAvailableControlToListByPosition(Skin sk, char position)
        {
            List<MyControl> listAllControls = GetAvailableControlToList(sk);
            List<MyControl> listResult = new List<MyControl>();

            foreach (MyControl item in listAllControls)
            {
                if (item.Position.Contains(position))
                {
                    listResult.Add(item);
                }
            }
            return listResult;
        }

        public static List<MyControl> GetAvailableControlToListByModule(Skin sk, int moduleID, char position)
        {
            List<MyControl> listAllControls = GetAvailableControlToList(sk);
            List<MyControl> listResult = new List<MyControl>();

            foreach (MyControl item in listAllControls)
            {
                if (item.Position.Contains(position) && item.ModuleID == moduleID)
                {
                    listResult.Add(item);
                }
            }
            return listResult;
        }

        private static List<ControlOnPage> GetListControlOnPage(Skin sk)
        {
            List<ControlOnPage> list = new List<ControlOnPage>();

            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                if (sk != null)
                {
                    foreach (var item in sk.SkinControls)
                    {
                        ControlOnPage cop = new ControlOnPage(item.MyControl, item.Position.Value, item.ControlOrder);
                        list.Add(cop);
                    }
                }
            }

            return list;
        }

        public static List<ControlOnPage> GetListControl(Skin sk, char position)
        {
            List<ControlOnPage> list = GetListControlOnPage(sk);
            List<ControlOnPage> listControl = new List<ControlOnPage>();

            foreach (var item in list)
            {
                if (item.Position == position)
                    listControl.Add(item);
            }

            return listControl;
        }

        public static void LoadAllControlToSkin(Page pg, PlaceHolder placeHoder, Skin sk, char position)
        {
            List<ControlOnPage> list = GetListControl(sk, position);
            list = list.OrderBy(i => i.Order).ToList();

            foreach (ControlOnPage item in list)
            {
                LoadControlToPage(pg, placeHoder, item);
            }
        }

        public static void LoadAllControlToSkinForAdmin(Page pg, PlaceHolder placeHoder, Skin sk, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                List<ControlOnPage> list = GetListControl(sk, position);
                list = list.OrderBy(i => i.Order).ToList();

                foreach (ControlOnPage item in list)
                {
                    LoadControlToPageForAdmin(pg, placeHoder, item);
                }
            }
        }

        public static void UpOrderControl(Skin sk, ControlOnPage control, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(c => c.Skin == sk && c.ControlID == control.ControlPage.ControlID && c.Position.Value == position);
                SkinControl sc2 = db.SkinControls.Where(c => c.Skin == sk && c.Position == position && c.ControlOrder < sc.ControlOrder).OrderByDescending(c => c.ControlOrder).FirstOrDefault();
                if (sc2 != null)
                {
                    int order = sc.ControlOrder;
                    sc.ControlOrder = sc2.ControlOrder;
                    sc2.ControlOrder = order;

                    db.SubmitChanges();
                }
            }
        }

        public static void DownOrderControl(Skin sk, ControlOnPage control, char position)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(c => c.Skin == sk && c.ControlID == control.ControlPage.ControlID && c.Position.Value == position);
                SkinControl sc2 = db.SkinControls.Where(c => c.Skin == sk && c.Position == position && c.ControlOrder > sc.ControlOrder).OrderBy(c => c.ControlOrder).FirstOrDefault();
                if (sc2 != null)
                {
                    int order = sc.ControlOrder;
                    sc.ControlOrder = sc2.ControlOrder;
                    sc2.ControlOrder = order;

                    db.SubmitChanges();
                }

            }
        }

        public static int GetMaxOrder(Skin sk, char position)
        {
            int max = 0;
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                var data = db.SkinControls.Where(p => p.Skin == sk && p.Position == position);
                if (data.Count() > 0)
                    max = data.Max(p => p.ControlOrder);
            }

            return max;
        }

        public static int GetMinOrder(Skin sk, char position)
        {
            int min = 0;
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                var data = db.SkinControls.Where(p => p.Skin == sk && p.Position == position);
                if (data.Count() > 0)
                    min = data.Min(p => p.ControlOrder);
            }

            return min;
        }

        public static void DeleteControl(Skin sk, string controlCode, char position, int totalControl)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(p => p.Skin == sk && p.MyControl.ControlCode == controlCode && p.Position == position);
                db.SkinControls.DeleteOnSubmit(sc);


                //Thay đổi vị trí các Control còn lại
                if ((sc.ControlOrder + 1) != totalControl)
                {
                    var listUpdate = db.SkinControls.Where(p => p.Skin == sk && p.Position == position && p.ControlOrder > sc.ControlOrder);

                    foreach (var item in listUpdate)
                    {
                        item.ControlOrder -= 1;
                    }
                }

                db.SubmitChanges();
            }
        }

        public static void MoveControlToLeft(Skin sk, ControlOnPage control)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(p => p.Skin == sk && p.ControlID == control.ControlPage.ControlID && p.Position == 'R');
                if (sc != null)
                {
                    int maxOrder = GetMaxOrder(sk, 'L');
                    sc.ControlOrder = maxOrder + 1;
                    sc.Position = 'L';
                    db.SubmitChanges();
                }
            }
        }
        public static void MoveControlToRight(Skin sk, ControlOnPage control)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(p => p.Skin == sk && p.ControlID == control.ControlPage.ControlID && p.Position == 'L');
                if (sc != null)
                {
                    sc.Position = 'R';

                    int maxOrder = GetMaxOrder(sk, 'R');
                    sc.ControlOrder = maxOrder + 1;

                    db.SubmitChanges();
                }
            }
        }

        public static void ReorderControlOnSkin(Skin sk, string controlCode, char oldPosition, char newPosition, int newOrder)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl sc = db.SkinControls.SingleOrDefault(s => s.Skin == sk && s.MyControl.ControlCode == controlCode);

                //su ly truong hop su ly tren page
                //Truong hop keo cung 1 ben
                //Kiem tra vi tri cu va moi
                var oldOrder = sc.ControlOrder;

                //Keo cung 1 ben

                if (oldPosition.Equals(newPosition))
                {
                    // Kéo lên trên
                    if (oldOrder > newOrder)
                    {
                        //Lay nhung control bi thay doi tu vi tri moi toi vi tri cu
                        var allControlChange = db.SkinControls.Where(s => s.Skin == sk && s.ControlOrder >= newOrder && s.ControlOrder < oldOrder);
                        foreach (var item in allControlChange)
                        {
                            item.ControlOrder = item.ControlOrder + 1;
                        }

                    }
                    //Khong thay doi vi tri
                    else if (oldOrder == newOrder)
                    {
                        //Khong co thay doi gi
                    }
                    // Kéo xuống dưới
                    else
                    {
                        //Lay nhung control bi thay doi tu vi tri cu toi vi tri moi
                        var allControlChange = db.SkinControls.Where(s => s.Skin == sk && s.ControlOrder <= newOrder && s.ControlOrder > oldOrder);
                        foreach (var item in allControlChange)
                        {
                            item.ControlOrder = item.ControlOrder - 1;
                        }
                    }
                }
                //Keo sang 2 ben
                else
                {

                    //Lay nhung control bi thay doi tu vi tri cu toi vi tri moi
                    //Do la nhung control nam phia duoi vi vi moi do
                    var allControlChangeNew = db.SkinControls.Where(s => s.Skin == sk && s.Position == newPosition && s.ControlOrder >= newOrder);
                    foreach (var item in allControlChangeNew)
                    {
                        item.ControlOrder = item.ControlOrder + 1;
                    }
                    //Lay nhung control o ben cu tu vi tri cua control keo xuong phia duoi
                    var allControlChangeOld = db.SkinControls.Where(s => s.Skin == sk && s.Position == oldPosition && s.ControlOrder > newOrder);
                    foreach (var item in allControlChangeOld)
                    {
                        item.ControlOrder = item.ControlOrder - 1;
                    }

                    sc.Position = newPosition;
                }

                sc.ControlOrder = newOrder;
                db.SubmitChanges();
            }
        }


        public static ControlOnPage GetControlOnPageByCode(Skin sk, string controlCode)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                SkinControl control = db.SkinControls.SingleOrDefault(s => s.Skin == sk && s.MyControl.ControlCode == controlCode);

                if (control != null)
                {
                    ControlOnPage cop = new ControlOnPage(control.MyControl, control.Position.Value, control.ControlOrder);
                    return cop;
                }
                else
                    return null;
            }
        }




        /// <summary>
        /// Phương thức thay đổi Skin
        /// </summary>
        /// <param name="page"></param>
        /// <param name="newSkin"></param>
        public static void ChangeSkinForPage(MyPage page, Skin newSkin)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                Skin oldSkin = page.Skin;

                var oldSkinControls = db.SkinControls.Where(sc => sc.SkinID == oldSkin.SkinID);

                if (oldSkin.Position.Contains('L') && oldSkin.Position.Contains('R'))
                {

                    foreach (SkinControl item in oldSkinControls)
                    {

                    }
                }
            }
        }

        #endregion

        public static string GetFullDateTime(object date)
        {
            DateTime d = Convert.ToDateTime(date);
            string full = "";
            switch (d.DayOfWeek)
            {
                case DayOfWeek.Monday: full += "Thứ Hai, "; break;
                case DayOfWeek.Tuesday: full += "Thứ Ba, "; break;
                case DayOfWeek.Wednesday: full += "Thứ Tư, "; break;
                case DayOfWeek.Thursday: full += "Thứ Năm, "; break;
                case DayOfWeek.Friday: full += "Thứ Sáu, "; break;
                case DayOfWeek.Saturday: full += "Thứ Bảy, "; break;
                case DayOfWeek.Sunday: full += "Chủ Nhật, "; break;
            }
            full += d.ToString("dd/MM/yyyy hh:mm tt");

            return full;

        }
    }
}